﻿using Canteen.Core.Models;
using Canteen.Core.Services;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using System;
using System.Threading.Tasks;

namespace Canteen.Core.WebApp.API.Controllers
{
    [Route("product")]
    [ApiController, Authorize]
    public class ProductController : ControllerBase
    {
        private readonly IProductService productService;
        private readonly IStoreService storeService;
        public ProductController(IProductService productService, IStoreService storeService)
        {
            this.productService = productService ?? throw new ArgumentNullException(nameof(productService));
            this.storeService = storeService ?? throw new ArgumentNullException(nameof(storeService));
        }

        [HttpGet("PageList")]
        public async Task<HttpResult<PageList<Product>>> GetPageList([FromQuery] ProductPageParameters parameters)
        {
            return HttpResult<PageList<Product>>.Success(await productService.GetPageList(parameters,storeService.StoreId));
        }

        [HttpGet("{id}")]
        public async Task<HttpResult<Product>> Get(int id)
        {
            return HttpResult<Product>.Success(await productService.SelectProductById(id));
        }

        [HttpGet]
        public async Task<HttpResult<Product>> Get([FromQuery] string code)
        {
            return HttpResult<Product>.Success(await productService.SelectProductByCode(code));
        }

        [HttpPost]
        public async Task<HttpResult<Product>> Post([FromBody] Product value)
        {
            value.StoreId = storeService.StoreId;
            return HttpResult<Product>.Success(await productService.CreateProduct(value));
        }
        [HttpPut]
        public async Task<HttpResult<Product>> Put([FromBody] Product value)
        {
            value.StoreId = storeService.StoreId;
            return HttpResult<Product>.Success(await productService.UpdateProduct(value));
        }
        [HttpDelete("{id}")]
        public async Task<HttpResult<bool>> Delete(int id)
        {
            return HttpResult<bool>.Success(await productService.DeleteProduct(id));
        }
    }
}
